# !/usr/bin python3                                 
# encoding: utf-8 -*-                            
# @author: 沙陌 微信：Matongxue_2
# @Time: 2021/10/20 20:02
# @Copyright：北京码同学
import os
import platform
import subprocess

from setting import DIR_NAME


class AppiumServer:

    # appium服务启动
    def start(self, port=4723, chromedriver_port=8000):
        # 模拟命令行动作
        appium_log_file = DIR_NAME + f'/logs/appium{port}.log'
        command = f'appium -p {port} --chromedriver-port {chromedriver_port} -g {appium_log_file}'

        # 调用命令行执行操作
        subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()

    # 停止服务
    def stop(self, port=4723):
        # 先查询对应端口占用的进程号，然后再杀死该进程，那么服务就停掉了
        system_name = platform.system()  # 获取系统名称
        if system_name.lower() == 'windows':
            res = os.popen(f'netstat -ano|findstr {port}')
            res_str = res.read().strip()  # 读取结果转换成字符串
            if 'LISTENING' in res_str:
                all_line = res_str.split('\n')
                for line in all_line:
                    # TCP    0.0.0.0:4723           0.0.0.0:0              LISTENING       9048
                    if 'LISTENING' in line:
                        process_id = line.split('LISTENING')[1].strip()  # 拿到进程id
                        os.popen(f'taskkill -f -pid {process_id}')   # 杀死进程
        else:
            # 按照mac的方式去执行
            # command pid user fd type device size/off node name
            # java 12345 username 36u ipv6 0x1234566abc oto tcp *:http-alt LISTEN
            res = os.popen('lsof -i:{}'.format(port))
            res_str = res.read().strip()
            if res_str != '' and '(LISTEN)' in res_str:
                all_line = res_str.split('\n')
                for i in range(1, len(all_line)):
                    line = all_line[i]
                    if '(LISTEN)' in line:
                        # 遍历所有行，判断是否为监听进程，如果是的话，就拿到进程号，杀死进程
                        process_id = line.split()[1]
                        print(process_id)
                        break
                os.popen('kill {}'.format(process_id))


if __name__ == '__main__':
    AppiumServer().start()
